package data.platform.cassandra.infra.persistence.mapping;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(value = "data_point")
public class DataPointEO {

    @PrimaryKey
    private DataPointKey dataPointKey;

    @Column("value")
    private Double value;
    // 事件值
    @Column("event_value")
    private String eventValue;

    public Object[] toDataPoint() {
        LocalDateTime localDateTime = LocalDateTime.of(dataPointKey.getPartition(), dataPointKey.getOffset());
        Date eventTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return new Object[]{eventTime.getTime(), value};
    }

    //  事件
    public Object[] toEventDataPoint() {
        LocalDateTime localDateTime = LocalDateTime.of(dataPointKey.getPartition(), dataPointKey.getOffset());
        Date eventTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return new Object[]{eventTime.getTime(), eventValue};
    }
}